package rs.networx.servlets;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import rs.networx.dao.PersonDao;
import rs.networx.entity.Person;
import rs.networx.entity.Post;

public class ViewWallServlet extends HttpServlet {

	private static final long serialVersionUID = 675143510789504200L;

	@EJB
	private PersonDao personDao;

	private Person loggedUser;

	private List<Post> loggedUsersOwnedPosts;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		System.out.println("LOADING: ViewWallServlet");

		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding(response.getCharacterEncoding());

		// dobavljamo sesiju i objekte sa sesije...
		HttpSession session = request.getSession();

		loggedUser = (Person) session.getAttribute("logovaniUser");

		try {
			System.out.println((personDao.findById(loggedUser.getId()))
					.getDob());
			loggedUser
					.setDob((personDao.findById(loggedUser.getId())).getDob());
		} catch (Exception e1) {
		}

		try {
			// dobavljamo 10 postova za logovanog korisnika...
			loggedUsersOwnedPosts = personDao.getFirstTenOwnedPosts(loggedUser
					.getId());
			System.out.println("loggedUsersOwnedPosts: "
					+ loggedUsersOwnedPosts.size());
		} catch (Exception e) {
		}

	
System.out.println(loggedUsersOwnedPosts);
		session.setAttribute("loggedUsersOwnedPosts", loggedUsersOwnedPosts);
		
		RequestDispatcher disp = request
				.getRequestDispatcher("ViewWall.jsp");
		disp.forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doGet(request, response);
	}


}
